package com.assist.common.utils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * Created by wuqiang on 2022/8/26 16:25
 */

public class JDKProxyUtil implements InvocationHandler {

    private MyHello hello;

    public JDKProxyUtil(MyHello hello) {
        this.hello = hello;
    }


    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("前置处理器");
        Object invoke = method.invoke(hello, args);
        System.out.println("后置处理器");
        return invoke;
    }

    public static void main(String[] args) {
        MyHello hello = new MyHello();
        JDKProxyUtil jdkProxyUtil = new JDKProxyUtil(hello);
        System.out.println(hello.getClass().getInterfaces().length);
        MyHello proxy =  (MyHello)Proxy.newProxyInstance(hello.getClass().getClassLoader(), hello.getClass().getInterfaces(), jdkProxyUtil);
        proxy.hello();
    }
}
